package common.utils.aliyun;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;

/**
 * 阿里云短信服务
 */
@Slf4j
public class AliyunSmsUtils {

	public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
		Config config = new Config()
				// 您的AccessKey ID
				.setAccessKeyId(accessKeyId)
				// 您的AccessKey Secret
				.setAccessKeySecret(accessKeySecret);
		// 访问的域名
		config.endpoint = "dysmsapi.aliyuncs.com";
		return new com.aliyun.dysmsapi20170525.Client(config);
	}

	/**
	 * 短信验证码
	 * @param mobileNo
	 * @param code
	 * @throws Exception
	 */
	public static void sendValidCode(String mobileNo, String code) throws Exception {
		com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5tAY1eZAcpWhZaXr5X5i", "fQz7rfx6vyLAfpvobtmahB0bXMSCg8");
		SendSmsRequest sendSmsRequest = new SendSmsRequest()
				.setPhoneNumbers(mobileNo)
				.setSignName("途盾新材料")
				.setTemplateCode("SMS_305015285")
				.setTemplateParam("{\"code\":\""+code+"\"}");
		// 复制代码运行请自行打印 API 的返回值
		client.sendSms(sendSmsRequest);

	}
}
